package com.haois.mp.database.module.offaccounts.service;

import com.haois.mp.database.module.common.daomain.RemindDob;
import com.haois.mp.database.module.common.daomain.RemindMem;
import com.haois.mp.database.module.offaccounts.dao.RemindMapper;
import com.haois.mp.database.util.jx.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * description
 *
 * @author RuiHaoZ on GuangZhou
 * @date 2018/8/4 14:51
 */
@Service
public class RemindDobService {
    @Autowired
    private RemindMapper remindMapper;

    public List<RemindDob> setOnBirthday(List<RemindDob> remindDobList) {
        remindDobList.forEach(remindDob ->
                setOnBirthday(remindDob));
        return remindDobList;
    }

    public RemindDob setOnBirthday(RemindDob remindDob) {
        if (RemindDob.LUNAR.equals(remindDob.getBirthdayType())) {
            Date previousBirthday = remindDob.lunarToSolarPreviousYear();
            Date thisBirthday = remindDob.lunarToSolarThisYear();
            Date nextBirthday = remindDob.lunarToSolarNextYear();
            if (DateUtil.yearsBetween(previousBirthday, new Date()) == 0) {
                remindDob.setOnBirthday(previousBirthday);
                remindDob.setNextBirthday(thisBirthday);
            } else {
                remindDob.setOnBirthday(thisBirthday);
                remindDob.setNextBirthday(nextBirthday);
            }
        } else if (RemindDob.SOLAR.equals(remindDob.getBirthdayType())) {
            remindDob.setOnBirthday(remindDob.transferThisYear().getTime());
            remindDob.setNextBirthday(remindDob.transferNextYear().getTime());
        }
        remindDob.setOnAge();
        return remindDob;
    }

    public List<RemindDob> findByMember(String memberNum,String beginTime,String endTime) {
        return remindMapper.findByMember(memberNum,beginTime,endTime);
    }

    public List<RemindDob> findNearByMember(String memberNum) {
        return remindMapper.findNearByMember(memberNum);
    }

    public List<RemindDob> findAll() {
        return remindMapper.findAll();
    }

    public List<RemindDob> find(RemindDob remindDob) {
        return remindMapper.find(remindDob);
    }

    public RemindDob findById(Integer id) {
        return setOnBirthday(remindMapper.findById(id));
    }

    @Transactional
    public void insert(RemindDob remindDob) {
        remindDob = setOnBirthday(remindDob);
        remindDob.setCreateTime(DateUtil.getYMdHm());
        remindMapper.insert(remindDob);
        remindMapper.insertRemindMem(remindDob.getId(), remindDob.getMemberNum(), 1);
    }

    public void update(RemindDob remindDob) {
        remindDob = setOnBirthday(remindDob);
        remindDob.setUpdateTime(DateUtil.getYMdHm());
        remindMapper.update(remindDob);
    }

    public List<RemindMem> findByBirId(int birId) {
        return remindMapper.findMemberByBirId(birId);
    }

}
